<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Laborline_Edit extends Page
{
       public $JSAJAX = null;
       public $hfForm = null;
       public $edRef = null;
       public $Label1 = null;
       public $JSRoundDecimal = null;
       public $hfwo = null;
       public $Button1 = null;
       public $hfType = null;
       public $JTDivDescription = null;
       public $JTGroupBox4 = null;
       public $btnAddDescription = null;
       public $btnDelDescription = null;
       public $edDescription = null;
       public $listDescription = null;
       public $Label2 = null;
       public $lblCost = null;
       public $btnCancel = null;
       public $btnUpdate = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $compid = null;
       public $btnShowDescription = null;
       public $lblNotes = null;
       public $memoNotes = null;
       public $cmbDescription = null;
       public $lblDescription = null;
       public $lblTotal = null;
       public $chkWarranty = null;
       public $edTotal = null;
       public $edCost = null;
       public $lblLaborUnits = null;
       public $edLaborUnits = null;
       public $cmbName = null;
       public $JTDate = null;
       public $cmbperformed = null;
       public $lblPerformed = null;
       public $hiddenVeh = null;
       public $Image1 = null;
       public $hiddenWo = null;
       public $hiddenLaborline = null;
       public $JTSiteTheme1 = null;
       function Button1JSClick($sender, $params)
       {

       ?>
       alert(document.getElementById('hiddenWo').value);

       <?php

       }


       function btnCancelJSClick($sender, $params)
       {

       ?>
       window.close();
       <?php

       }


       function btnDelDescriptionJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var DescIndex = document.getElementById('listDescription').selectedIndex;
       var DescValue = document.getElementById('listDescription')[DescIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxDesc3(DescValue,comp);
       <?php

       }

       function btnAddDescriptionJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var adddesc = document.getElementById('edDescription').value;
       var compider = document.getElementById('compid').value;
       AjaxDesc2(adddesc,compider);
       document.Vehicle_Laborline_Edit.edDescription.value = "";
       <?php

       }

       function btnShowDescriptionJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivDescription').Show();
        document.Vehicle_Laborline_Edit.edDescription.value = "";
       <?php

       }
       function cmbperformedChange($sender, $params)
       {
       $performed = $this->cmbperformed->getItemIndex();
       if($performed=='employee')
       {
       $this->cmbName->Clear();
       $result_emp = mysql_query("select * from employee_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_emp = mysql_fetch_array($result_emp))
       {
            $this->cmbName->AddItem($row_emp['lname'].','.$row_emp['fname'], null, $row_emp['lname'].','.$row_emp['fname']);
       }
       }
       else
       {
       $this->cmbName->Clear();
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbName->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }
       }


       }

       function btnCancelClick($sender, $params)
       {
       redirect('veh_laborline_list.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWo->getValue());
       }


       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }



       function Vehicle_Laborline_EditCreate($sender, $params)
       {
       $this->compid->Value = $_SESSION['company_id'];
       $veh = $_GET['veh'];
       $WO = $_GET['WO'];
       if($veh!=null || $veh!='')
       $veh_laborline = $_GET['laborline'];
       if($veh!=null || $veh!='')
       $this->hiddenVeh->setValue($veh);
       if($WO!=null || $WO!='')
       $this->hiddenWo->setValue($WO);
       if($veh_laborline!=null || $veh_laborline!='')
       $this->hiddenLaborline->setValue($veh_laborline);

       $type = $_GET['type'];
       if($type!=null || $type!='')
       $this->hfType->setValue($type);

       $form = $_GET['form'];
       if($form!=null || $form!='')
       $this->hfForm->setValue($form);

       if($_SESSION['access_vehicle']==1)
       {
       $this->btnCancel->Visible=false;
       $this->btnUpdate->Visible=false;
       }

       if($_SESSION['access_vehicle']==2)
       {
       $this->btnCancel->Visible=true;
       $this->btnUpdate->Visible=true;
       }


       $query_labor = "select * from veh_laborline_tbl where workorder='".$this->hiddenWo->getValue()."'and labor_id='".$this->hiddenLaborline->getValue()."'";
       $result_labor = mysql_query($query_labor)or die(mysql_error());
       $row_labor = mysql_fetch_array($result_labor);

       if($row_labor['warranty']=="1")
       {
       $this->chkWarranty->Checked=true;
       }
       else
       {
       $this->chkWarranty->Checked=false;
       }

       $this->cmbperformed->setItemIndex($row_labor['performed_by']);

       $this->cmbName->Clear();
       $employee = $row_labor['employee'];
       $this->cmbName->AddItem($employee, null, $employee);
       if($this->cmbperformed->ItemIndex=='employee')
       {
       $result_emp = mysql_query("select * from employee_tbl where company_id='".$_SESSION['company_id']."' and job_title='Fleet Technician'");

       while($row_emp = mysql_fetch_array($result_emp))
       {
            $this->cmbName->AddItem($row_emp['lname'].','.$row_emp['fname'], null, $row_emp['lname'].','.$row_emp['fname']);
       }
       }
       else
       {
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbName->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }
       }

       $this->JTDate->Text=$row_labor['labor_date'];
       $this->edLaborUnits->Text = $row_labor['labor_unit'];
       $this->edRef->Text = $row_labor['reference_num'];
       $this->edCost->Text = $row_labor['cost'];
       $this->edTotal->Text = $row_labor['total'];
       $this->memoNotes->Text = $row_labor['notes'];

       $this->cmbDescription->Clear();
       $this->cmbDescription->ItemIndex=$row_labor['description'];
       $result_desc = mysql_query("select value from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' and category='task descriptions'");

        while($row_desc = mysql_fetch_array($result_desc))
        {
            $this->cmbDescription->AddItem($row_desc['value'], null, $row_desc['value']);
        }

        //list description
        $result_d = mysql_query("select value from lookup_workorder_tbl where company_id='".$_SESSION['company_id']."'and category='task descriptions'");

        while($row_d = mysql_fetch_array($result_d))
        {
            $this->listDescription->AddItem($row_d['value'], null, $row_d['value']);
        }

       }


       function btnUpdateClick($sender, $params)
       {
       if($this->chkWarranty->Checked==true)
       {
       $warranty='1';
       }
       else
       {
       $warranty='0';
       }

       $query_update = "UPDATE veh_laborline_tbl SET
       performed_by='".$this->cmbperformed->getItemIndex()."',
       employee='".$this->cmbName->getItemIndex()."',
       reference_num = '".$this->edRef->Text."',
       labor_date='".$this->JTDate->Text."',
       labor_unit='".$this->edLaborUnits->Text."',
       cost='".$this->edCost->Text."',
       total='".$this->edTotal->Text."',
       description='".$this->cmbDescription->getItemindex()."',
       notes='".$this->memoNotes->Text."',
       warranty='".$warranty."'
       WHERE workorder='".$this->hiddenWo->getValue()."'and labor_id ='".$this->hiddenLaborline->getValue()."'";

       if($this->hfForm->getValue()=='summary')
       {
       $this->hfwo->setValue($this->hiddenWo->getValue());
       $type = $this->hfType->getValue();
       if($type=='edit')
       {
       $filename = "temp/laborline_update.txt";
       $handle = fopen($filename, "w");
       fwrite($handle, $query_update);
       fclose($handle);

       echo "<script type=text/javascript>
       var wo = ".$this->hiddenWo->getValue().";
       var veh= ".$this->hiddenVeh->getValue()."
       var laborline = ".$this->hiddenLaborline->getValue()."
       window.location.href='labor_endorsement.php?WO='+wo+'&veh='+veh+'&laborline='+laborline;
       </script>
       ";
       }
       if($type=='create')
       {
       mysql_query($query_update) or die(mysql_error());

       //redirect('veh_laborline_list.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWo->getValue());
       echo '<script type="text/javascript">
       //alert(window.opener.parent.location.href);
       window.opener.parent.setWODetails();
       window.opener.JTFrameLabor.location.reload();
       alert("Successfully Updated Record!");
       window.close();
       </script>';
       }
       }

       if($this->hfForm->getValue()=='order')
       {
       mysql_query($query_update) or die(mysql_error());

       echo '<script type="text/javascript">
       window.opener.parent.setWODetails();
       //window.opener.JTFrameLabor.location.reload();
       window.opener.location.reload();
       alert("Successfully Updated Record!");
       window.close();
       </script>';
       }

       }

       function edCostJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.Vehicle_Laborline_Edit.edTotal.value = roundNumber((document.Vehicle_Laborline_Edit.edLaborUnits.value) * (document.Vehicle_Laborline_Edit.edCost.value),2);


       //accept only numbers
       var numero = document.getElementById("edCost").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edCost").value = numero;

       <?php

       }

       function edLaborUnitsJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.Vehicle_Laborline_Edit.edTotal.value = roundNumber((document.Vehicle_Laborline_Edit.edLaborUnits.value) * (document.Vehicle_Laborline_Edit.edCost.value),2);
       <?php

       }



}

global $application;

global $Vehicle_Laborline_Edit;

//Creates the form
$Vehicle_Laborline_Edit=new Vehicle_Laborline_Edit($application);

//Read from resource file
$Vehicle_Laborline_Edit->loadResource(__FILE__);

//Shows the form
$Vehicle_Laborline_Edit->show();

?>